/**
 * Created by d on 2017/5/17.
 */

var detailId = null;//详情页的ID  作为加载详情页的
//登录
var login = (function (win,undef){

    var loginButton = $('.js-login-login')[0];

    var username = $(".js-login-username");
    var password = $(".js-login-password");

    var checkForm = function () {
        return true;
    };
    var checkPassword = function () {
        var data = {'username':username.val(),'password':password.val()};
        ajax('l2.php',data,function (res) {
            if (res)
            {
                $.router.loadPage("/order_list.html");  //加载ajax页面
            }
        } ,'登录中');
    };
    var login = (function () {
        var init = function () {
            bindButton();
        };

        var bindButton = function () {
            $(loginButton).click(function () {
                if (!checkForm())
                {
                    return false;
                }
                checkPassword();
            });
        };

        return {
            init:init
        };
    })();

    return {
        init:function () {
            login.init();
        }
    }

})(window);

var order_list = (function () {
    var init = function (page) {
        // ajax('list');
        //初始化方法必须要做的事情   声明节点变量赋值
        listItem = $('.list-container');
        refresh(page); //绑定刷新方法
        addItems(20,0);
    };

    var listItem = null;
    var loadConfig = {
        loading : false, //加载锁
        maxItems : 100, //当前页最大加载数量
        itemsPerLoad : 10,//每页加载数量
        lastIndex : 20 //当前最后index
    };



    var addItems = function(number, lastIndex) {
        var html = '';
        for (var i = lastIndex + 1; i <= lastIndex + number; i++) {
            html += '<a href="./detail_list.html?id=1">';
            html += '<li class="item-content item-link">';
            html += '<div class="item-media"><i class="icon icon-cart"></i></div>';
            html += '<div class="item-inner">';
            html += '<div class="item-title">2017051837584</div>';
            html += '<div class="item-after">paid</div>';
            html += '</div>';
            html += '</li>';
            html += '</a>';

        }
        listItem.append(html);

    }
    var refresh = function (page) {

        $('#order_list').on('infinite', '.infinite-scroll',function() {
            // 如果正在加载，则退出
            if (loadConfig.loading) return;

            // 设置flag
            loadConfig.loading = true;
            //todo:ajax获取数据刷新
            setTimeout(function() {
                loadConfig.loading = false;

                if (loadConfig.lastIndex >= loadConfig.maxItems) {
                    $.detachInfiniteScroll($('.infinite-scroll'));
                    $('.infinite-scroll-preloader').remove();
                    return;
                }

                addItems(loadConfig.itemsPerLoad, loadConfig.lastIndex);
                loadConfig.lastIndex = $('.list-container li').length;
            }, 1000);
        });
    };
    return {
        init:function (page) {
            init(page);
        }
    }
})();

var detail_list = (function () {
    var init = function () {
        // ajax();获得详情的商品信息

    };
})();
var detail = (function () {

    var init = function () {
        $image = $('#image');
        bindCropper();
    };

    var bindCropper = function () {
        $image.cropper(cropper_option);
    };
    var URL = window.URL || window.webkitURL;
    var $image = null;
    var uploadedImageURL;
    
    var cropper_option = {
        aspectRatio: NaN,
        preview: '.img-preview',
        crop: function (e) {}
    };
    return {
        init : init
    }
})();

login.init();



//每次加载新页面时候初始化新页面的闭包
$(document).on("pageInit", function(e, pageId, $page) {
    var initFun = pageId + '.init()';
    eval(initFun);

});




